<?php
class FriendsManagerController extends CustomControllerAction {
    protected $identity = null;
    public function init() {
        parent::init();
        $this->breadcrumbs->addStep('用户账户', $this->getUrl(null, 'account'));
        $this->breadcrumbs->addStep('好友管理', $this->getUrl(null, 'friendmanager'));
        
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect($this->getUrl('login', 'account'));
        }
        $this->identity = $auth->getIdentity();
    }
    
	public function indexAction() {
		$friends = DatabaseObject_User::GetFriends($this->db, 
		      $this->identity->userid, 1);
		$friendsTobe = DatabaseObject_User::GetFriends($this->db, 
		      $this->identity->userid, 0);
		
		$tobefriends = DatabaseObject_User::GetFriendRequest($this->db,
		      $this->identity->userid);
		
		$this->view->count1 = count($friends);
		$this->view->friends = $friends;
		
		$this->view->count2 = count($friendsTobe);
		$this->view->friendsTobe = $friendsTobe;
		
		$this->view->count3 = count($tobefriends);
		$this->view->tobefriends = $tobefriends;
	}
	
	public function operateAction() {
		$request = $this->getRequest();
		$action = $request->getQuery('action');
		$id = $request->getQuery('id');
		if (strlen($action) != 0 && strlen($id) != 0) {
			$user = new DatabaseObject_User($this->db);
			if ($user->load($this->identity->userid)) {
				if ($action == 'add') {
					if ($user->addFriend((int)$id)) {
						$this->messager->addMessage('好友添加成功');
					} else {
						$this->messager->addMessage('好友添加失败');
					}
				} elseif ($action == 'del') {
					if ($user->delFriend((int)$id)) {
						$this->messager->addMessage('好友删除成功');
					} else {
						$this->messager->addMessage('好友删除失败');
					}
				} elseif ($action == 'status') {
					if ($user->statusFriend((int)$id)) {
						$this->messager->addMessage('验证好友成功');
					} else {
						$this->messager->addMessage('验证好友失败');
					}
				}
				$this->_redirect($this->getUrl('index'));
			} else {
				$this->messager->addMessage('请求用户不存在');
				$this->_redirect($this->getUrl('index'));
			}
			
		} else {
			$this->_redirect($this->getUrl('index'));
		}
	}
}
?>